package com.lx.web.servlet;


import com.lx.bean.Comment;
import com.lx.bean.CommentReply;
import com.lx.bean.ResultInfo;
import com.lx.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    CommentService service;

    @RequestMapping("/commentBykey/{cid}")
    @ResponseBody
    public ResultInfo selectBycommentBykey(@PathVariable Integer cid) {
        ResultInfo info = new ResultInfo();
        List<Comment> comments = service.selectBywherebyCommentandreply(cid);
        if (comments != null && comments.size() > 0) {
            info.setFlag(true);
            info.setData(comments);
        } else {
            info.setFlag(false);
            info.setErrorMsg("没有查询到相关数据！");
        }
        return info;
    }

    @RequestMapping("/addcomment")
    public ResultInfo addComment(Comment comment) {
        ResultInfo info = new ResultInfo();
//        if(comment.getContent()==null){
//            info.setFlag(false);
//            info.setErrorMsg("您回复的内容为空");
//            return  info;
//        }
//        Integer addcomment = service.addcomment(comment);
//        if (addcomment > 0) {
//            info.setFlag(true);
//        } else {
//            info.setFlag(false);
//        }
        return info;
    }

    @RequestMapping("/addcommentreply")
    public ResultInfo addCommentReply(CommentReply reply) {
        ResultInfo info = new ResultInfo();
        if(reply.getContent()==null){
            info.setFlag(false);
            info.setErrorMsg("您回复的内容为空");
            return  info;
        }
        Integer addcomment = service.addCommentReply(reply);
        if (addcomment > 0) {
            info.setFlag(true);
        } else {
            info.setFlag(false);
            info.setErrorMsg("回复内容失败，请稍后再试！");
        }
        return info;
    }

}
